//
//  ViewController.m
//  JYTableBaseAdaptorTest
//
//  Created by 应明顺 on 11/20/18.
//  Copyright © 2018 JackYing. All rights reserved.
//

#import "ViewController.h"
#import "JYTableAdaptor.h"
#import "JYTableBaseAdaptor+EditDecorate.h"

@interface ViewController ()
//// 自定义adaptor --- 1
@property (nonatomic, strong) JYTableBaseAdaptor *adaptor;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.topMargin = 16;
    
    [self.adaptor regsiterCell:[JYBaseInputCell class] forModelClass:[JYBaseInputModel class]];
    [self configTableAdaptor];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // cell左滑按钮选择项适配图片（上下方向）
    [self.adaptor configSwipeButtons];
}

//// 自定义adaptor --- 2
- (JYTableBaseAdaptor *)adaptor {
    if (!_adaptor) {
        _adaptor = [JYTableBaseAdaptor tableAdaptorWithTableView:self.tableView title:@"演示" key:@"demo"];
        _adaptor.dataArr = [[self configTableViewDataSource] mutableCopy];
        _adaptor.cellEditable = ^BOOL(NSIndexPath *indexPath, JYTableBaseAdaptor *adaptor) {
            return YES;
        };
        _adaptor.cellEditAction = [self tableViewCellEditAction];
        _adaptor.cellEditIcon = @[[UIImage imageNamed:@"doubt"], [UIImage imageNamed:@"pic"]];
    }
    return _adaptor;
}

- (NSArray *)configTableViewDataSource {
    
    JYBaseCellModel *name = [JYBaseCellModel modelWithItemKey:@"name" title:@"姓名" content:@"JackYing"];
    name.estimatedHeight = 150;
    JYBaseInputModel *phone = [JYBaseInputModel modelWithItemKey:@"phone" title:@"手机号" content:@"18112345678"];
    JYBaseInputModel *msgCode = [JYBaseInputModel modelWithItemKey:@"msgCode" title:@"验证码" placeholder:@"请输入您收到的验证码"];
    
    JYBaseGroupModel *group1 = [JYBaseGroupModel groupModelWithRowModelArr:@[name, phone, msgCode]];
    group1.headerModel = [JYBaseHeaderModel modelHeaderWithTitle:@"快速注册" subTitle:@"请录入正确的手机号"];
    group1.footerModel = [JYBaseFooterModel modelFooterWithTitle:@"下一步"];
    
    JYBaseGroupModel *group2 = [JYBaseGroupModel groupModelWithRowModelArr:@[name, phone, msgCode]];
    group2.headerModel = [JYBaseHeaderModel modelHeaderWithTitle:@"快速注册" subTitle:@"请录入正确的手机号"];
    group2.footerModel = [JYBaseFooterModel modelFooterWithTitle:@"下一步"];
    
    return @[group1, group2, group2];
}

- (void)configAdaptorAction {
    
    __weak typeof(self) weakSelf = self;
    self.adaptor.nextAcvtion = ^(id action) {
        if ([action isEqualToString:@"next"]) {
            __strong typeof(weakSelf) self = weakSelf;
            NSLog(@"点击了第二步");
            [self pushNextPage];
        }
    };
    
    self.adaptor.cellSelectedAction = ^(NSIndexPath *indexPath, JYTableBaseAdaptor *adaptor, JYBaseCellModel *model) {
            NSLog(@"点击cell ==> indexPath:%@\nmodel:%@", indexPath, model);
    };
}

- (void)pushNextPage {
    [super pushNextPage];
    
    if ([self verifyDataSourceContent]) {
        
        
    }
}


#pragma mark - cellEditAction
- (CellEditAction)tableViewCellEditAction {
    return ^NSArray<UITableViewRowAction *> * _Nullable(NSIndexPath *indexPath, JYTableBaseAdaptor *adaptor, JYBaseCellModel *model) {
        
        UITableViewRowAction *unbind = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleNormal) title:@"解绑" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            NSLog(@"点击%@", action.title);
        }];
        
        UITableViewRowAction *edit = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleNormal) title:@"编辑" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            NSLog(@"点击%@", action.title);
        }];
        
        
        return @[edit, unbind];
    };
}

@end
